<?php
namespace app\manage\controller;

class AnalysisGameKeep extends Base
{
    public function index(){
        if(request()->isAjax() || input('?export')){
            $plat = input('plat');
            $date = explodeTime(input('date'));
            $gameId = input('game_id');

            !$gameId && $this->layTable();

            $keep = model('Analysis')->keepPrice($plat, $date['start_time'], $date['end_time'], $gameId, 'keep');

            //导出
            if(input('?export')){
                array_unshift($keep, ['新增日期', '游戏ID', '游戏名称', '上线日期', '当日新增', '2日留存', '7日留存', '30日留存', '60日留存', '90日留存']);
                exportExcel($keep, input('date').'留存率', input('date').'留存率');
            }

            $this->layTable(0, '游戏分析', $keep, count($keep));
        }else{
            $date = date('Y-m-d', time()) . ' - ' . date('Y-m-d', time());
            return view('', ['date'=>$date]);
        }
    }

    public function chart(){
        $plat = input('plat');
        $startTime = strtotime(input('date'));
        $endTime = $startTime+86399;
        $gameId = input('game_id');

        $findGame = model('Analysis')->oneKeepPrice($plat, $startTime, $endTime, $gameId, 'keep');
        $dateType = ['当日新增', '2日', '3日', '4日', '5日', '6日', '7日', '14日', '30日', '60日', '90日'];

        return view('', ['series'=>$findGame, 'date_type'=>json_encode($dateType)]);
    }

    public function time(){
        $onlineTimeRegion = model('ViewGameManage')->where('game_id', input('game_id'))->value('online_time_region');
        $arr = explode('-', $onlineTimeRegion);
        if(isset($arr[0])){
            if($arr[0]=='' || $arr[0]==0){
                $time = '';
            }else{
                $time =  date('Y-m-d', $arr[0]) . ' - ' . date('Y-m-d', $arr[0]);
            }
        }else{
            $time = '';
        }

        $this->layJson(0, '上线时间', $time);
    }
}